<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleAdd"
        >新增团员</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="aUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="头像" align="center" prop="headImg" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.headImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="角色" align="center" prop="role" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.agent_level" :value="scope.row.role"/>
        </template>
      </el-table-column>
      <el-table-column label="下载数量" align="center" prop="downloadNum" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="setTeam(scope.row)"
                >设置团员</el-button>
              </template>
            </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listAUser, getAUser, delAUser, addAUser, updateAUser } from "@/api/system/aUser";
import { addTeamInfors } from "@/api/system/aTeamInfo";


export default {
  name: "AUser",
  dicts: ['agent_level'],
  data() {
    return {
      teamId:"",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小程序用户表格数据
      aUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isShow: null,
        numSoft: null,
        miniOpenId: null,
        openId: null,
        phone: null,
        nickName: null,
        headImg: null,
        realName: null,
        role: null,
        downloadNum: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.teamId = this.$route.query.teamId;
    this.getList();
  },
  methods: {
    /** 查询小程序用户列表 */
    getList() {
      this.loading = true;
      this.queryParams.role = 2;//默认查询普通用户 0代理商 1团队成员2普通用户
      listAUser(this.queryParams).then(response => {
        this.aUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        delFlag: null,
        isShow: null,
        numSoft: null,
        miniOpenId: null,
        openId: null,
        phone: null,
        nickName: null,
        headImg: null,
        realName: null,
        role: null,
        downloadNum: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 增加团员按钮操作 */
    handleAdd(row) {
      var ids = row.id || this.ids;
      ids = ids+","+this.teamId;
      console.log(ids)
      this.$modal.confirm('是否添加当前人员为团员？').then(function() {
        return addTeamInfors(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("添加成功");
      }).catch(() => {});
    },
  }
};
</script>
